package dao

import (
	"encoding/json"
)

type UserOrganization struct {
	BaseModel
	UserId         int
	OrganizationId int
	CompanyId      int
}

func (c *UserOrganization) TableName() string {
	return "user_organization"
}

func (c *UserOrganization) ToJsonStr() string {
	marshal, err := json.Marshal(c)
	if err != nil {
		return ""
	}
	return string(marshal)
}

func SelectAllUserOrganization() *[]UserOrganization {
	infoAuth := make([]UserOrganization, 0)
	DB.Where("is_del = ?", 0).Find(&infoAuth)
	return &infoAuth
}

func SelectUserOrganizationByExample(userOrg *UserOrganization) *[]UserOrganization {
	organizations := make([]UserOrganization, 0)
	DB.Where(&userOrg)
	DB.Find(&organizations)
	return &organizations
}

func SaveUserOrganization(userOrg *UserOrganization) int {
	DB.Create(&userOrg)
	return userOrg.ID
}

func SaveUserOrganizationBatch(userOrgs *[]UserOrganization) {
	for _, organizations := range *userOrgs {
		DB.Create(&organizations)
	}
}

func UpdateUserOrganization(userOrg *UserOrganization) {
	DB.Model(&userOrg).Save(&userOrg)
}

func DeleteUserOrganizationById(id int) {
	userOrganization := UserOrganization{}
	userOrganization.ID = id
	DB.Delete(userOrganization)
}

func DeleteUserOrganizationByExample(userOrg *UserOrganization) {
	DB.Where(&userOrg).Delete(UserOrganization{})
}
